/**
 * @(#) StringMath.java     2012/09/11 15:24
 */
package org.fly.utils;


import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
 * @version 1.0
 */
public final class StringNumber {

    // RoundingMode.HALF_UP  ��������
    // RoundingMode.HALF_DOWN  Ĩ��


    private static MathContext getMathContext() {
        //20��ʾȡ20λ��Ч���֣�RoundingMode.HALF_UP��ʾ��������
        MathContext mathContext = new MathContext(20, RoundingMode.HALF_UP);
        return mathContext;
    }

    //��
    public static String add(String number1, String number2) {
        if(StringUtil.isNullOrEmpty(number1)) {
            number1 = "0";
        }
        if(StringUtil.isNullOrEmpty(number2)) {
            number2 = "0";
        }
        BigDecimal decimal = new BigDecimal(number1);
        decimal = decimal.add(new BigDecimal(number2));
        BigDecimal d = decimal.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return d.toString();
        //return decimal.toString();
    }

    //��
    public static String sub(String number1, String number2) {
        if(StringUtil.isNullOrEmpty(number1)) {
            number1 = "0";
        }
        if(StringUtil.isNullOrEmpty(number2)) {
            number2 = "0";
        }
        BigDecimal decimal = new BigDecimal(number1);
        decimal = decimal.subtract(new BigDecimal(number2));
        BigDecimal d = decimal.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return d.toString();
    }

    //��
    public static String mul(String number1, String number2) {
        if(StringUtil.isNullOrEmpty(number1)) {
            number1 = "0";
        }
        if(StringUtil.isNullOrEmpty(number2)) {
            number2 = "0";
        }
        BigDecimal decimal = new BigDecimal(number1);
        decimal = decimal.multiply(new BigDecimal(number2), getMathContext());
        BigDecimal d = decimal.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return d.toString();
    }

    //��
    public static String div(String number1, String number2) {
        if(StringUtil.isNullOrEmpty(number1)) {
            number1 = "0";
        }
        if(StringUtil.isNullOrEmpty(number2) || compareTo(number2,"0") == 0) {
            number2 = "0";
            return "0";
        }
        BigDecimal decimal = new BigDecimal(number1);
        decimal = decimal.divide(new BigDecimal(number2), getMathContext());
        BigDecimal d = decimal.setScale(2,BigDecimal.ROUND_HALF_DOWN);
        return d.toString();
    }

    //����ֵ
    public static String abs(String number) {
        if (number == null || number.trim().isEmpty()) return "0";
        return new BigDecimal(number).abs(getMathContext()).toString();
    }

    //��ֵ�� (thisn), ����ȷ�������ֵ��ʹ��������޾��ȡ�
    public static String pow(String number, int n) {
        return new BigDecimal(number).pow(n).toString();
    }

    //ȡ��
    public static String negate(String number) {
        return new BigDecimal(number).negate().toString();
    }

    //�Ƿ��ܹ�����
    public static boolean isExactDivision(String a, String b) {
        try {
            Integer.parseInt(new BigDecimal(a).divide(new BigDecimal(b)).toString());
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    //�Ƿ��ܹ����� ����a��b �� c��
    public static boolean isExactDivisionBs(String a, String b, int c) {
        try {
            int n = Integer.parseInt(new BigDecimal(a).divide(new BigDecimal(b)).toString());
            if(n >= c) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    /**
     * 去除小数位， 四舍五入
     *
     * @param number
     * @return
     */
    public static String formatPrecisionOfZero(String number) {
        return new BigDecimal(number).setScale(0, RoundingMode.HALF_UP).toString();
    }


    //���number1����number2 �򷵻�1 ��� �򷵻�0 С���򷵻� -1
    public static int compareTo(String number1, String number2) {
        if(StringUtil.isNullOrEmpty(number1)) {
            number1 = "0";
        }
        if(StringUtil.isNullOrEmpty(number2)) {
            number2 = "0";
        }
        return new BigDecimal(number1).compareTo(new BigDecimal(number2));
    }

    //�ж�num�Ƿ���min��max֮��
    public static boolean isBetween(String num, String min, String max) {
        if (compareTo(num, min) >= 0 && compareTo(num, max) <= 0) {
            return true;
        } else {
            return false;
        }
    }


    //�õ������İٷֱ�
    public static String getPercent(String num) {
        return new BigDecimal(num).divide(new BigDecimal(100)).toString();
    }

    //�õ�������ǧ�ֱ�
    public static String getPerMil(String num) {
        return new BigDecimal(num).divide(new BigDecimal(1000)).toString();
    }

    //��ʽ�� ����С�����2λ ������������
    public static String format(String number) {
        if (StringUtil.isNullOrEmpty(number)) {
            return "0";
        }
        return new BigDecimal(number).setScale(2, RoundingMode.HALF_UP).toString();
    }

    //��ʽ�� ָ������С��������λ ���ҽ�����������
    public static String format(String number, int pointCount) {
        if (StringUtil.isNullOrEmpty(number)) {
            return "0";
        }
        return new BigDecimal(number).setScale(pointCount, RoundingMode.HALF_UP).toString();
    }

    //Ĩ��λ��һ������
    public static String rounding(String number, int pointCount) {
        return new BigDecimal(number).setScale(pointCount, RoundingMode.DOWN).toString();
    }

    //���ָ�ʽ�� ����С�����2Ϊ �������Ϊ�� �򷵻�ΪĬ��ֵ defaultValue
    public static String format(String number, String defaultValue) {
        if (StringUtil.isNullOrEmpty(number)) {
            return defaultValue;
        }
        return format(number);
    }

    public static String format(String number,int pointCount,RoundingMode mode){
        if (StringUtil.isNullOrEmpty(number)) {
            return "0";
        }
        return new BigDecimal(number).setScale(pointCount, mode).toString();
    }

    public static String nullToZero(String number) {
        return StringUtil.isNullOrEmpty(number) ? "0" : number;
    }
}
